package com.ssm.student.controller;

import com.ssm.student.eo.PersonEoPage;
import com.ssm.student.model.Person;
import com.ssm.student.service.PersonService;
import com.ssm.student.tool.ResponseMessage;
import com.ssm.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "person")
@RestController
@CrossOrigin
public class PersonController {
    @Autowired
    private PersonService personService;
    @ApiOperation(value = "添加方法")
    @RequestMapping(value = "/addPerson", method = RequestMethod.GET)
    public ResponseMessage addPerson(Person person) {
        int IsAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (person != null) {
            //(2)名称不能为空
            if (person.getName1() != null) {
                //(3)添加数据
                IsAdd = personService.AddPerson(person);
                if (IsAdd == 1) {
                    return Result.success("保存成功");
                } else if (IsAdd == -1) {
                    return Result.error("姓名重复，不能添加");
                } else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }

    @ApiOperation(value = "删除方法")
    @RequestMapping(value = "/delPerson", method = RequestMethod.GET)
    public ResponseMessage delPerson(int personid) {
        if (personid != 0) {
            int delInt = 0;
            delInt = personService.DeletePerson(personid);
            if (delInt > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } else {
            return Result.error("删除的主键不能为0请重新输入");
        }
    }

    @ApiOperation(value = "修改方法")
    @RequestMapping(value = "/updatePerson", method = RequestMethod.GET)
    public ResponseMessage updatePerson(Person person) {
        //传入的参数不能为空
        if (person != null) {
            //参数中主键不能为0
            if (person.getPersonid() != 0) {
                if (personService.UpdatePerson(person) == 1) {
                    //
                    if (person.getName1()!=null){
                        if (person.getEmal().length()<5){
                            return Result.success("更新成功");
                        }
                    }
                }
            }
        }
        else {
            return Result.error("更新传入参数不正确");
        }
        return Result.error("更新传入参数不正确");
    }
    @ApiOperation(value = "根据条件获取信息")
    @RequestMapping(value = "/getAllPerson",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
    private  ResponseMessage getAllPerson(@RequestBody PersonEoPage personEoPage){
        Map<String, List> map=new HashMap<>();
        List<PersonEoPage> listperson=personService.getWhereAll(personEoPage);
        map.put("list",listperson);
        return Result.success("200",map);
    }
    @ApiOperation(value = "根据条件获取信息")
    @RequestMapping(value = "/getPersonSome",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    private  ResponseMessage getPersonSome(@RequestParam String loginaccount, String password){
        Map<String, List> map=new HashMap<>();
        List<PersonEoPage> listperson1=personService.getWhereSome(loginaccount,password);
        if (listperson1!=null) {
            map.put("list", listperson1);
            return Result.success("200",map);
        }else {
            return Result.error("用户名或密码错误");
        }

    }
}
